package com.mobartisan.workinglunch.network;

import android.content.Context;


import com.mobartisan.workinglunch.SnakeApplication;
import com.mobartisan.workinglunch.network.interceptor.CookiesInterceptor;
import com.mobartisan.workinglunch.network.interceptor.IgnoreSSLTrustManager;

import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.X509TrustManager;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * <p>
 * This is the main entry point for network communication. Use this class for instancing REST services which do the
 * actual communication.
 */
public class BaseApiService {
    private static final String TAG = BaseApiService.class.getSimpleName();
    /**
     * This is our main backend/server URL.
     */
    public static String REST_API_URL = "https://www.pgyer.com/apiv1/app/install?aId=607cc75e0ad15db800b430f56d6de0d1&_api_key=f2dc44eeac000e437bc9de7f6844b87b";
    private static Retrofit s_retrofit;
    private static OkHttpClient client;

    static {
        Context context = SnakeApplication.getInstance();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        CookiesInterceptor cookiesInterceptor = new CookiesInterceptor(context);
        File cacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache");
        Cache cache = new Cache(cacheDirectory, 50 * 1024 * 1024);
        //X509TrustManager trustAllCert = new SelfSignedTrustManager(CLIENT_TRUST_P);
        X509TrustManager trustAllCert = new IgnoreSSLTrustManager();
        // final SSLSocketFactory sslSocketFactory = new SSLSocketFactoryCompat(trustAllCert);
        try {
            client = new OkHttpClient.Builder()
                    .addInterceptor((Interceptor.Chain chain) -> {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/json; charset=UTF-8")
                                .addHeader("Accept-Encoding", "gzip, deflate")
                                .addHeader("Connection", "keep-alive")
                                .addHeader("Accept", "*/*")
                                .addHeader("Cookie", "add cookies here")
                                .build();
                        return chain.proceed(request);
                    })
                    .addInterceptor(interceptor)
                    //.addNetworkInterceptor(new StethoInterceptor())
                    .addInterceptor(cookiesInterceptor)
                    .connectTimeout(600, TimeUnit.SECONDS)
                    .writeTimeout(600, TimeUnit.SECONDS)
                    .readTimeout(600, TimeUnit.SECONDS)
                    .cache(cache)
                    //.sslSocketFactory(sslSocketFactory)
                    //.sslSocketFactory(getSSLSocketFactory(context))
                    .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        s_retrofit = new Retrofit.Builder()
                .baseUrl(REST_API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//用于返回Rxjava调用,非必须
                .client(client)
                .build();
    }
    public static <T> T getService(Class<T> serviceClass) {
        return s_retrofit.create(serviceClass);
    }
}
